package cn.xiyue.production.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;

public abstract class IBaseAdapter<T> extends BaseAdapter {

    List<T> list;

    public IBaseAdapter(List<T> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public T getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder<T> vh;
        if (convertView == null){
            vh = getViewHolder();
            convertView = vh.init(parent);
            convertView.setTag(vh);
        }else {
            vh = (ViewHolder<T>) convertView.getTag();
        }
        return vh.setData(list.get(position));
    }

    public abstract ViewHolder<T> getViewHolder();

    public interface ViewHolder<T>{
        View setData(T data);

        View init(ViewGroup root);
    }
}
